package com.lbj.googleplay.googleplay100.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.TypedValue;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.lbj.googleplay.googleplay100.http.protocol.HotProtocol;
import com.lbj.googleplay.googleplay100.ui.fly.FlowLayout;
import com.lbj.googleplay.googleplay100.ui.view.LoadingPage;
import com.lbj.googleplay.googleplay100.ui.view.MyFLowLayout;
import com.lbj.googleplay.googleplay100.utils.DrawableUtils;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import java.util.ArrayList;
import java.util.Random;

/*
 * 排行
 * */
public class HotFragment extends BaseFragment {


    private ArrayList<String> data;

    @Override
    public View onCreateSuccessView() {
        ScrollView scrollView = new ScrollView(UIUtils.getContext());
        MyFLowLayout flowLayout = new MyFLowLayout(UIUtils.getContext());
        int padding = UIUtils.dip2px(10);
        flowLayout.setPadding(padding,padding,padding,padding);//设置内边距
//        flowLayout.setHorizontalSpacing(UIUtils.dip2px(6));//水平间距
//        flowLayout.setVerticalSpacing(UIUtils.dip2px(8));//竖直间距
        scrollView.addView(flowLayout);
        for (int i = 0; i <data.size() ; i++) {
            TextView view = new TextView(UIUtils.getContext());
            final String keyword = data.get(i);
            view.setText(keyword);
            view.setTextColor(Color.WHITE);
            view.setTextSize(TypedValue.COMPLEX_UNIT_SP,UIUtils.dip2px(18));
            view.setPadding(padding,padding,padding,padding);
            //随机颜色 30-230之间
            Random random = new Random();
            int r = random.nextInt(200)+30;
            int g = random.nextInt(200)+30;
            int b = random.nextInt(200)+30;
            //生成item的背景图片
            int color = 0xffcecece;//按下颜色
            StateListDrawable selector = DrawableUtils.getSelector(Color.rgb(r, g, b), color, UIUtils.dip2px(6));
            //设置背景图片
            view.setBackgroundDrawable(selector);
            flowLayout.addView(view);
            //设置点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(UIUtils.getContext(), keyword, Toast.LENGTH_SHORT).show();
                }
            });
        }
        return scrollView;
    }

    @Override
    public LoadingPage.ResultState onLoad() {
        HotProtocol protocol = new HotProtocol();
        data = protocol.getData(0);
        return check(data);
    }
}
